package com.example.piece.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.base.aop.click.RepeatClick
import com.example.base.aop.login.LoginCallback
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.ext.getViewModel
import com.example.base.ext.navigationTo
import com.example.base.ui.fragment.BaseFragment
import com.example.base.vm.onError
import com.example.piece.App
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.FragmentFollowBinding
import com.example.piece.entity.RecommendEntity
import com.example.piece.entity.UserAttentionListEntity
import com.example.piece.ui.adapter.HomeFollowAdapter
import com.example.piece.vm.HomeViewModel
import com.example.piece.vm.UserViewModel
import com.kongzue.dialogx.dialogs.TipDialog

/**
 * 关注
 */
class FollowFragment : BaseFragment<FragmentFollowBinding>() {

    companion object {
        fun newInstance(): Fragment {
            val fragment = FollowFragment()
            return fragment
        }
    }

    private val homeViewModel: HomeViewModel by lazy {
        getViewModel<HomeViewModel>()
    }
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }

    /**  类型 Adapter  **/
    private val homeFollowAdapter: HomeFollowAdapter by lazy {
        val adapter = HomeFollowAdapter(emptyList())
        mBinding.rv.adapter = adapter
        adapter
    }

    override fun getContentRes(): Int = R.layout.fragment_follow


    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        mBinding.fragment = this
        recommend()
        homeFollowAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                navigationTo(RoutePath.MAIN_INFO_ACTIVITY, mutableMapOf(
                    Key.DataKey.INFO_TYPE to 0,
                    Key.DataKey.TARGET_USER_ID to (adapter.getItem(position) as RecommendEntity).userId))
            }

        })

        homeFollowAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int,
            ) {

                when (view.id) {
                    R.id.tv_read -> {
                        userAttention((adapter.getItem(position) as RecommendEntity).userId,
                            position,
                            (adapter.getItem(position) as RecommendEntity).isAttention)

                    }
                }
            }

        })
        mBinding.srl.setOnRefreshListener { recommend() }

        mBinding.rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    Glide.with(this@FollowFragment).resumeRequests();
                } else {
                    Glide.with(this@FollowFragment).pauseRequests();
                }
            }
        })

    }

    @RepeatClick
    fun fresh() {
        recommend()
    }

    /**  获取主页的推荐关注数据  **/
    private fun recommend() {
        homeViewModel.recommend(this) {
            mBinding.srl.isRefreshing = false
            if (it.code == 200) {
                homeFollowAdapter.setList(it.data)

            } else {
                TipDialog.show(it.msg)

            }
        } onError {
            mBinding.srl.isRefreshing = false
            TipDialog.show(it)
        }
    }

    /*  *
       * 用户关注
       * @param status  1 关注 0 取消关注
       * @param userId  用户id*/
    @LoginCallback
    private fun userAttention(userId: String, position: Int, attention: Boolean) {
      /*  if (App.USER_TOKEN.isNullOrEmpty()) {
            toast("请登录")
        } else {*/

            userViewModel.userAttention(this, if (attention) "0" else "1", userId) {
                if (it.code == 200) {
                    homeFollowAdapter.data.get(position).isAttention =
                        !homeFollowAdapter.data.get(position).isAttention
                    homeFollowAdapter.notifyDataSetChanged()
                } else {
                    toast(it.msg)
                }
            } onError {
                toast(it)

            }
        }


    //}


}